package com.example.demo;

import java.util.Date;

import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.pojo.CommonJSONResult;
import com.example.pojo.SysUser;
import com.example.service.SysUserService;

@RestController
public class SysUserController {

	@Autowired
	private SysUserService userService;
	
	@Autowired
	private Sid sid;
	
	@PostMapping("/saveUser")
	public CommonJSONResult saveUser() {
		String userId = sid.nextShort();
		SysUser user = SysUser.builder().id(userId).age(18).password("123456").name("刘文钊" + new Date()).createDate(new Date()).build();
		userService.insertUser(user);
		return CommonJSONResult.ok("保存成功！");
	}
	
	@PostMapping("/updateUser")
	public CommonJSONResult updateUser() {
		SysUser user = SysUser.builder().id("1001").age(18).password("123456").name("刘文钊变帅了" + new Date()).createDate(new Date()).build();
		userService.updateUser(user);
		return CommonJSONResult.ok("更新成功！");
	}
	
	@GetMapping("/getUserById")
	public CommonJSONResult getUserById(String userId) {
		return CommonJSONResult.ok(userService.getUserById(userId));
	}
	
	@GetMapping("/getUserByExample")
	public CommonJSONResult getUserByExample(String userId) {
		return CommonJSONResult.ok(userService.getUserByExample(userId));
	}
	
	@PostMapping("/getUserByPage")
	public CommonJSONResult getUserByPage(@RequestBody SysUser user, Integer currentPage, Integer pageSize) {
		return CommonJSONResult.ok(userService.getUserByPage(user, currentPage, pageSize));
	}
	
	@GetMapping("/listUser")
	public CommonJSONResult listUser() {
		return CommonJSONResult.ok(userService.listUser());
	}
	
	@GetMapping("/getUserCustom")
	public CommonJSONResult getUserCustom() {
		return CommonJSONResult.ok(userService.getCustomUser());
	}
	
	
	
}
